package com.indigon.utils
{	
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	
	public class SoundManager
	{
		private static var _instance : SoundManager;
		
		private var musicLoops    : uint;            
		private var musicSound    : Sound;           
		private var musicChannel  : SoundChannel;    
		private var soundVolume   : SoundTransform;  
		private var musicVolume   : SoundTransform;  
		private var globalOn      : Boolean;         
		private var soundOn       : Boolean;         
		private var musicOn       : Boolean;         
		private var sounds        : Dictionary;      
		
		public function SoundManager()
		{
		}
		
		public function init():void
		{
			soundOn       = true;
			musicOn       = true;
			globalOn      = true;
			sounds        = new Dictionary();
			soundVolume   = new SoundTransform(1);
			musicVolume   = new SoundTransform(1);
		}
		
		public function playRandomSound(array:Array, volume:Number=NaN, loops:uint=0):SoundChannel
		{
			var rand : uint = Math.random() * array.length;
			return playSoundByClass(array[rand], volume, loops);
		}
		
		public function playSoundByClass(className:Class, volume:Number=NaN, loops:uint=0):SoundChannel
		{
			var sound : Sound = sounds[className];
			
			if (sound == null)
			{
				sound = new className() as Sound;
				sounds[className] = sound;
			}
			
			return sound.play(0, loops, isNaN(volume) ? soundVolume : new SoundTransform(volume));
			
		}
		
		public function playMusicByClass(className:Class, volume:Number=NaN, loops:uint=0):SoundChannel
		{
			if (musicChannel) musicChannel.stop();
			var channel : SoundChannel = playSoundByClass(className, volume, loops);
			if (isNaN(volume))
			{
				musicVolume = new SoundTransform(1);
			}
			else
			{
				musicVolume = new SoundTransform(volume);
			}
			musicChannel = channel;
			musicSound   = sounds[className];
			musicLoops   = loops;
			return channel;
		}
		
		public function stopMusic():void
		{
			if (musicChannel)
			{
				musicChannel.stop();
				musicChannel = null;
				musicSound   = null;
			}
		}
		
		public function setGlobalSound(value:Boolean):void
		{
			globalOn = value;
			if (!globalOn)
			{
				SoundMixer.soundTransform = new SoundTransform(0);
			}
			else
			{
				SoundMixer.soundTransform = new SoundTransform(1);
			}
		}
		
		public function setSound(value:Boolean):void
		{
			soundOn = value;
			if (!soundOn)
			{
				soundVolume.volume = 0;
			}
			else
			{
				soundVolume.volume = 1;
			}
		}
		
		public function setMusic(value:Boolean):void
		{
			musicOn = value;
			if (!musicOn)
			{
				if (musicChannel) musicChannel.stop();
			}
			else
			{
				if (musicChannel)
				{
					musicSound.play(0, musicLoops, musicVolume);
				}
			}
		}
		
		public static function get instance():SoundManager
		{
			if (_instance == null) 
			{
				_instance = new SoundManager();
			}
			return _instance;
		}
	}
}